package app

import (
	"context"

	"charging_pile/common/ctxdata"
	"charging_pile/common/xerr"
	"charging_pile/models"
	"charging_pile/user/internal/svc"
	"charging_pile/user/internal/types"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetWalletLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetWalletLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWalletLogic {
	return &GetWalletLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetWalletLogic) GetWallet() (resp *types.Wallet, err error) {
	uid := ctxdata.GetUidFromCtx(l.ctx)
	logx.Info(uid)
	wallet, err := l.svcCtx.Models.Wallet.FindOneByUserId(l.ctx, uid)

	if err != nil && err != models.ErrNotFound {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取钱包信息失败")
	}
	if wallet == nil {
		return &types.Wallet{}, nil
	}

	return &types.Wallet{
		Cash: int(wallet.Cash),
		Card: 0,
		Gift: int(wallet.Gift),
	}, nil
}
